package cn.zh.npro.Activity;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.shenyuanqing.zxingsimplify.zxing.Activity.CaptureActivity;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.npro.Adapter.GPadpter;
import cn.zh.npro.Adapter.Gpladapter;
import cn.zh.npro.Adapter.RpAdapter;
import cn.zh.npro.Item.CITem;
import cn.zh.npro.Item.GLItem;
import cn.zh.npro.Item.OrItem;
import cn.zh.npro.Item.RpItem;
import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import cn.zh.npro.Util.Loading;
import cn.zh.npro.Util.MyListener;
import cn.zh.npro.Util.PullToRefreshLayout;
import cn.zh.npro.Util.PullableListView;
import cn.zh.npro.Util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class RepOd extends BaseActivity implements PullableListView.OnLoadListener {
    ImageView back;
    int page=1;
    LinearLayout gslin;
    String key="";
    ImageView seleimg,wima;
    TextView empty,stxt;
    PopupWindow popupWindow,popupWindowgtype;;
    ArrayList<GLItem> galist=new ArrayList<GLItem>();
    //当前商品标签
    GLItem glItem=null;
    GPadpter gPadpter;
    TextView gdclass,gttxt;
    RelativeLayout pore;
    //筛选
    //判断弹窗是否可以删除
    boolean isdele=true;
    Gpladapter gpladapter;

    //弹窗队列
    ArrayList<PopupWindow> popus=new ArrayList<PopupWindow>();
    //商品类别数据
    ArrayList<CITem>ciTems=new ArrayList<CITem>();
    //记录返回的商品数据
    CITem fciTem=null;

    String gdtype="0",flagId="",classId="";

    PullableListView pulist;
    PullToRefreshLayout refresh_view;
    private Dialog LDialog;

    RpAdapter rpAdapter=null;
    ArrayList<RpItem> rpItems=new ArrayList<RpItem>();

    TextView total,look;
    ArrayList<RpItem> trpItems=new ArrayList<RpItem>();
    private static final int REQUEST_SCAN = 0;
    ImageView rpscan;
    String type="0";
    OrItem orItem=null;
    String CustomerID;
    JSONArray Trade_GoodsList;
    MyApplication mapp;
    String customerID;
    String clientId;
    public static final int INIT = 0;
    public static final int STOP = 2;
    //弹窗弹出使背景变暗
    View popview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rep_od);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        mapp= (MyApplication) getApplication();
        type=getIntent().getStringExtra("type");



        if (type.equals("3")){
            key=getIntent().getStringExtra("key");


         }
        if (type.equals("1")){
           customerID = getIntent().getStringExtra("clId");
            orItem= (OrItem) getIntent().getSerializableExtra("ob");
            getOrded();
         }else {
            customerID = mapp.getClID();
            getgood(page+"");
           }
        initrp();
         }


       public void initrp(){
           back=findViewById(R.id.back);
           seleimg=findViewById(R.id.seleimg);
           rpscan=findViewById(R.id.rpscan);
           wima=findViewById(R.id.wima);
           gslin=findViewById(R.id.gslin);
           stxt=findViewById(R.id.stxt);
           total=findViewById(R.id.total);
           look=findViewById(R.id.look);
           popview=findViewById(R.id.popview);
           refresh_view=(PullToRefreshLayout)findViewById(R.id.refresh_view);
           empty= (TextView)findViewById(R.id.empty);
           pulist= (PullableListView)findViewById(R.id.pulist);
           View view=findViewById(R.id.activity_rep_od);
           Activity activity = RepOd.this;
           while (activity.getParent() != null) {
               activity = activity.getParent();
           }
           rpAdapter=new RpAdapter(activity,rpItems,total,0,view,popview,customerID);
           pulist.setAdapter(rpAdapter);
           pulist.setOnLoadListener(this);
           gslin.setOnClickListener(this);
           back.setOnClickListener(this);
           seleimg.setOnClickListener(this);
           wima.setOnClickListener(this);
           look.setOnClickListener(this);
           rpscan.setOnClickListener(this);
           refresh_view.setOnRefreshListener(new MyListener(){
               @Override
               public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                   super.onRefresh(pullToRefreshLayout);
                   //下拉刷新
                   page=1;
                   getgood(page+"");
                 }
              });
            }

    @Override
    public void onLoad(PullableListView pullableListView) {
        page=page+1;
        getgood(page+"");
      }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.back:
                finish();
                break;
            case R.id.gslin:
                Intent intent=new Intent(RepOd.this,GsActivity.class);
                intent.putExtra("type",1);
                startActivityForResult(intent,0);
                break;
            case R.id.wima:
                stxt.setText("客户名称");
                key="";
                page=1;
//                getgood(page+"");
                wima.setVisibility(View.GONE);
                break;
            case R.id.seleimg:
                showPopUp();
                break;
            case R.id.gasure:
                page=1;
                String str=gdclass.getText().toString();
                if (str.equals("全部")){
                    glItem=null;
                    fciTem=null;
                 }
                popupWindow.dismiss();

                //此处加需判断
                getgood(page+"");
                break;
            case R.id.clean:
                page=1;
                gdclass.setText("全部");
                for (int j=0;j<galist.size();j++){
                    galist.get(j).setType(0);
                }
                gPadpter.notifyDataSetChanged();
                fciTem=null;
                glItem=null;
                popupWindow.dismiss();
                getgood(page+"");
                break;
            case R.id.gdtype:
                typePo();
                break;
            case R.id.gachose:
                getPo();
                getclss();
                break;
            case R.id.gall:
                popupWindowgtype.dismiss();
                gdtype="0";
                gttxt.setText("全部");
                break;
            case R.id.gup:
                popupWindowgtype.dismiss();
                gdtype="1";
                gttxt.setText("上架");
                break;
            case R.id.gdown:
                popupWindowgtype.dismiss();
                gdtype="2";
                gttxt.setText("下架");
                break;
            case R.id.look:
                trpItems.clear();
                for (int i=0;i<rpItems.size();i++){
                    if (rpItems.get(i).getNum()!=0){
                        trpItems.add(rpItems.get(i));
                     }
                  }
                  if (trpItems.size()==0){
                    ToastUtil.showLong(RepOd.this,"请选择商品");
                    return;
                  }
             Intent intent1=new Intent(RepOd.this,DeOrder.class);
             intent1.putExtra("tlist",trpItems);
             intent1.putExtra("type",type);
             intent1.putExtra("customerID",customerID);

             startActivity(intent1);
             break;
            case R.id.rpscan:
                getRuntimeRight();
                break;
            case R.id.poview:
                popupWindow.dismiss();
                break;
             }
          }


    /**
     * 获得运行时权限
     */
    private void getRuntimeRight() {
        if (ContextCompat.checkSelfPermission(RepOd.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(RepOd.this, new String[]{Manifest.permission.CAMERA}, 1);
        } else {
            jumpScanPage();
         }
       }


    /**
     * 跳转到扫码页
     */
    private void jumpScanPage() {
        startActivityForResult(new Intent(RepOd.this, CaptureActivity.class),REQUEST_SCAN);
    }




    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case 2:
                String name=data.getStringExtra("name");
                key=name;
                stxt.setText(name);
                page=1;
                getgood(page+"");
                wima.setVisibility(View.VISIBLE);
                break;
               }
        if(requestCode == REQUEST_SCAN && resultCode == RESULT_OK){
            page=1;
            String str=gdclass.getText().toString();
            if (str.equals("全部")){
                glItem=null;
                fciTem=null;
            }

            key=data.getStringExtra("barCode");
            //此处加需判断
            getgood(page+"");

             }

            }





    //侧滑弹窗
    private void showPopUp(){
        gPadpter = new GPadpter(this,galist);
        getLabel();
        //修正后代码，适用于一个或多个parent的情形
        Activity activity = RepOd.this;
        while (activity.getParent() != null) {
            activity = activity.getParent();
        }
        View vp = LayoutInflater.from(activity).inflate(R.layout.ga_window, null, false);
        gdclass = vp.findViewById(R.id.gdclass);
        gttxt = vp.findViewById(R.id.gttxt);
        if (null!=fciTem){
            gdclass.setText(fciTem.getClassName());
        }else {
            gdclass.setText("全部");
        }
        if (gdtype.equals("0")){
            gttxt.setText("全部");
        }else if (gdtype.equals("1")){
            gttxt.setText("上架");
        }else if (gdtype.equals("2")){
            gttxt.setText("下架");
        }
        LinearLayout gdtype=vp.findViewById(R.id.gdtype);
        GridView gridView=vp.findViewById(R.id.gagrid);
        Button gasure=vp.findViewById(R.id.gasure);
        Button clean=vp.findViewById(R.id.clean);
        View poview=vp.findViewById(R.id.poview);
        final LinearLayout gachose=vp.findViewById(R.id.gachose);
        pore = vp.findViewById(R.id.pore);
        gasure.setOnClickListener(this);
        clean.setOnClickListener(this);
        gachose.setOnClickListener(this);
        gdtype.setOnClickListener(this);
        poview.setOnClickListener(this);
        gridView.setAdapter(gPadpter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (int j=0;j<galist.size();j++){
                    if (j==i){
                        galist.get(j).setType(1);
                        glItem=galist.get(j);
                    }else {
                        galist.get(j).setType(0);
                    }
                }
                gPadpter.notifyDataSetChanged();
            }
                        });

        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int  mheigh = outMetrics.heightPixels;

        popupWindow = new PopupWindow(vp, LinearLayout.LayoutParams.MATCH_PARENT, mheigh);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.getContentView().setFocusableInTouchMode(true);
        popupWindow.getContentView().setFocusable(true);
        popupWindow.setClippingEnabled(false);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        popupWindow.showAtLocation(findViewById(R.id.activity_rep_od), Gravity.NO_GRAVITY, 0, 0);
        }

    public void typePo(){
        //修正后代码，适用于一个或多个parent的情形
        Activity activity = RepOd.this;
        while (activity.getParent() != null) {
            activity = activity.getParent();
        }
        View vpa = LayoutInflater.from(activity).inflate(R.layout.gtype, null, false);
        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int  mScreenWitdh = outMetrics.widthPixels;
        int lweith=mScreenWitdh/5;
        final ImageView gaa=vpa.findViewById(R.id.gty_back);
        ImageView duione=vpa.findViewById(R.id.duione);
        ImageView duitwo=vpa.findViewById(R.id.duitwo);
        ImageView duithree=vpa.findViewById(R.id.duithree);
        duione.setVisibility(View.GONE);
        duitwo.setVisibility(View.GONE);
        duithree.setVisibility(View.GONE);
        switch (gdtype){
            case "0":
                duione.setVisibility(View.VISIBLE);
                break;
            case "1":
                duitwo.setVisibility(View.VISIBLE);
                break;
            case "2":
                duithree.setVisibility(View.VISIBLE);
                break;
               }
        LinearLayout gall=vpa.findViewById(R.id.gall);
        LinearLayout gup=vpa.findViewById(R.id.gup);
        LinearLayout gdown=vpa.findViewById(R.id.gdown);

        gall.setOnClickListener(this);
        gup.setOnClickListener(this);
        gdown.setOnClickListener(this);

        popupWindowgtype = new PopupWindow(vpa, mScreenWitdh-lweith, LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindowgtype.setFocusable(true);
        popupWindowgtype.setOutsideTouchable(true);
        popupWindowgtype.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindowgtype.getContentView().setFocusableInTouchMode(true);
        popupWindowgtype.getContentView().setFocusable(true);
        popupWindowgtype.setAnimationStyle(R.style.popwin_anim_style);
        gaa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindowgtype.dismiss();
            }
        });

        popupWindowgtype.showAtLocation(findViewById(R.id.activity_rep_od), Gravity.NO_GRAVITY, lweith, 0);
        }



    public void getPo(){
        isdele=true;
        Activity activity =RepOd.this;
        while (activity.getParent() != null) {
            activity = activity.getParent();
        }
        View vpa = LayoutInflater.from(activity).inflate(R.layout.gaa_layout, null, false);
        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int  mScreenWitdh = outMetrics.widthPixels;
        int lweith=mScreenWitdh/5;
        final ImageView gaa=vpa.findViewById(R.id.gaa_back);

        ListView gaa_list=vpa.findViewById(R.id.gaa_list);

        gpladapter = new Gpladapter(RepOd.this,ciTems);
        gaa_list.setAdapter(gpladapter);

        gaa_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if (null==ciTems.get(i).getChildClass()||ciTems.get(i).getChildClass().length()==0){
                    fciTem=ciTems.get(i);
                    gdclass.setText(fciTem.getClassName());
                    popus.get(0).dismiss();
                }else {
                    getPodata(ciTems.get(i).getChildClass());
                }
            }
        });
        final PopupWindow popupWindowga = new PopupWindow(vpa, mScreenWitdh-lweith, LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindowga.setFocusable(true);
        popupWindowga.setOutsideTouchable(true);
        popupWindowga.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindowga.getContentView().setFocusableInTouchMode(true);
        popupWindowga.getContentView().setFocusable(true);
        popupWindowga.setAnimationStyle(R.style.popwin_anim_style);
        gaa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindowga.dismiss();
            }
        });

        popupWindowga.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (isdele) {
                    if (popus.size()>0) {
                        popus.remove((popus.size() - 1));
                    }
                }
              }
        });

        popus.add(popupWindowga);
        popupWindowga.showAtLocation(findViewById(R.id.activity_rep_od), Gravity.NO_GRAVITY, lweith, 0);
         }


    //获取商品类别请求
    public void getclss(){
        MyApplication mapp= (MyApplication) getApplication();
        SharedPreferences sp = mapp.getSp();
        OkHttpUtils.get(BaseUrl.getGoodsClass+"?UserId="+ sp.getString("uid",""))
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean state=js.getBoolean("State");
                            if (state){
                                ciTems.clear();
                                JSONArray ja=js.getJSONArray("Res");
                                CITem ciTem0=new CITem("","全部","","",null);
                                ciTems.add(ciTem0);
                                for (int i=0;i<ja.length();i++){
                                    JSONObject ji=ja.getJSONObject(i);
                                    String ClassID=ji.getString("ClassID");
                                    String ClassName=ji.getString("ClassName");
                                    String FatherId=ji.getString("FatherId");
                                    String ClassID2=ji.getString("ClassID2");
                                    JSONArray ChildClass=ji.getJSONArray("ChildClass");
                                    CITem ciTem=new CITem(ClassID,ClassName,FatherId,ClassID2,ChildClass);
                                    ciTems.add(ciTem);
                                }
                                gpladapter.notifyDataSetChanged();

                            }else {
                                String msg=js.getString("msg");
                                ToastUtil.showLong(RepOd.this,msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                      }
                  });
              }


    public void getPodata(JSONArray ja){
        final ArrayList<CITem> arrciTems=new ArrayList<CITem>();
        for (int i=0;i<ja.length();i++){
            try {
                JSONObject ji=ja.getJSONObject(i);
                String ClassID=ji.getString("ClassID");
                String ClassName=ji.getString("ClassName");
                String FatherId=ji.getString("FatherId");
                String ClassID2=ji.getString("ClassID2");
                JSONArray ChildClass=ji.getJSONArray("ChildClass");
                CITem ciTem=new CITem(ClassID,ClassName,FatherId,ClassID2,ChildClass);
                arrciTems.add(ciTem);
            }catch (JSONException e) {
                e.printStackTrace();
            }
        }
        Activity activity =RepOd.this;
        while (activity.getParent() != null) {
            activity = activity.getParent();
        }
        View vpa = LayoutInflater.from(activity).inflate(R.layout.gaa_layout, null, false);
        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);

        int  mScreenWitdh = outMetrics.widthPixels;
        int lweith=mScreenWitdh/5;
        ImageView gaa=vpa.findViewById(R.id.gaa_back);
        ListView gaa_list=vpa.findViewById(R.id.gaa_list);

        Gpladapter gpladapter = new Gpladapter(this,arrciTems);
        gaa_list.setAdapter(gpladapter);
        gaa_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (arrciTems.get(i).getChildClass().length()==0){
                    fciTem=arrciTems.get(i);
                    gdclass.setText(fciTem.getClassName());
                    isdele=false;
                    for (int j=0;j<popus.size();j++){
                        popus.get(j).dismiss();
                    }
                    popus.clear();
                }else {
                    getPodata(arrciTems.get(i).getChildClass());
                }
            }
        });
        final PopupWindow popupWindowga = new PopupWindow(vpa, mScreenWitdh-lweith, LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindowga.setFocusable(true);
        popupWindowga.setOutsideTouchable(true);
        popupWindowga.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindowga.getContentView().setFocusableInTouchMode(true);
        popupWindowga.getContentView().setFocusable(true);
        popupWindowga.setAnimationStyle(R.style.popwin_anim_style);

        gaa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindowga.dismiss();
            }
        });
        popupWindowga.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (isdele) {
                    popus.remove((popus.size() - 1));
                }
            }
        });
        popus.add(popupWindowga);
        popupWindowga.showAtLocation(findViewById(R.id.activity_rep_od), Gravity.NO_GRAVITY, lweith, 0);
        }


    //网络请求
    //获取商品标签
    public  void getLabel(){
        galist.clear();
        OkHttpUtils.get(BaseUrl.GetGoodsLabel)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean status=js.getBoolean("State");
                            if (status){
                                JSONArray ja=js.getJSONArray("Res");
                                for (int i=0;i<ja.length();i++){
                                    JSONObject ji=ja.getJSONObject(i);
                                    String FlagID=ji.getString("FlagID");
                                    String FlagType=ji.getString("FlagType");
                                    String FlagName=ji.getString("FlagName");
                                    String BGColor=ji.getString("BGColor");
                                    String FontColor=ji.getString("FontColor");
                                    GLItem glItem=new GLItem(0,FlagID ,FlagType,FlagName,BGColor,FontColor);
                                    galist.add(glItem);
                                 }
                                gPadpter.notifyDataSetChanged();
                            }else {
                                String msg=js.getString("msg");
                                ToastUtil.showLong(RepOd.this,msg);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                     }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                  });
                }



    public void getgood(final String page){
        if (null!=glItem){
            flagId=glItem.getFlagID();
        }else {
            flagId="";
        }
        if (null!=fciTem){
            classId=fciTem.getClassID();
        }else {
            classId="";
        }
        if (page.equals("1")){
            LDialog= Loading.createLoadingDialog(this,"加载中...");
          }
        MyApplication mapp= (MyApplication) getApplication();
        SharedPreferences sp = mapp.getSp();
        OkHttpUtils.get(BaseUrl.getGoodsPlus+"?UserId="+ sp.getString("uid",""))
                .params("page",page+"")
                .params("size","7")
                .params("key",key)
                .params("flagId",flagId)
                .params("bBlockUp",gdtype)
                .params("classId",classId)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("------------>获取商品列表==="+s);
                       if (null!=LDialog){
                           Loading.closeDialog(LDialog);
                          }
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State) {
                                JSONArray Res = js.getJSONArray("Res");
                                if (page.equals("1")){
                                    rpItems.clear();
                                    if (0==Res.length()){
                                        refresh_view.setVisibility(View.INVISIBLE);
                                        empty.setVisibility(View.VISIBLE);
                                        pulist.finishLoading(INIT);
                                        return;
                                    }else {
                                        refresh_view.setVisibility(View.VISIBLE);
                                        empty.setVisibility(View.GONE);
                                      }
                                }else {
                                    if (Res.length()==0){
                                        pulist.finishLoading(STOP);
                                        return;
                                    }
                                  }
                                for (int i = 0; i < Res.length(); i++) {
                                    JSONObject ji = Res.getJSONObject(i);
                                    String GoodsId = ji.getString("GoodsId");
                                    String GoodsName = ji.getString("GoodsName");
                                    String GoodsNo = ji.getString("GoodsNo");
                                    String IndexURL = ji.getString("IndexURL");
                                    String Price_Detail = ji.getString("Price_Detail");
                                    String Unit = ji.getString("Unit");
                                    RpItem rpItem = new RpItem(GoodsId, GoodsName, GoodsNo, IndexURL, Price_Detail, Unit,0,"","","","",0.0,0.0,0.0,0.0);
                                    if (type.equals("1")){
                                        if (page.equals("1")) {
                                            JSONArray ja = Trade_GoodsList;
                                            if (null==Trade_GoodsList){
                                                return;
                                              }
                                            for (int g = 0; g < ja.length(); g++) {
                                                JSONObject jg = ja.getJSONObject(g);
                                                int SellCount = jg.getInt("SellCount");
                                                String GoID = jg.getString("GoodsID");
                                                if (rpItem.getGoodsId().equals(GoID)) {
                                                    rpItem.setNum(SellCount);
                                                }
                                              }
                                             }
                                          }
                                    rpItems.add(rpItem);
                                   }
                                rpAdapter.notifyDataSetChanged();
                                pulist.finishLoading(INIT);
                            }else {
                                String msg=js.getString("msg");
                                ToastUtil.showLong(RepOd.this,msg);
                                if (page.equals("1")){
                                    pulist.finishLoading(INIT);
                                }else {
                                    pulist.finishLoading(STOP);
                                }

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
            }




    public void getOrded(){
        OkHttpUtils.get(BaseUrl.GetTradeDetail)
                .params("tradeId",orItem.getTradeID())
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                JSONObject Res=js.getJSONObject("Res");
                                JSONObject Trade_TradeList=Res.getJSONObject("Trade_TradeList");
                                CustomerID = Trade_TradeList.getString("CustomerID");
                                Trade_GoodsList = Res.getJSONArray("Trade_GoodsList");
                                page=1;
                                getgood(page+"");
                                mapp.setClID(CustomerID);
                            }else {
                                String Msg=js.getString("Msg");
                                ToastUtil.showLong(RepOd.this,Msg);
                                refresh_view.setVisibility(View.INVISIBLE);
                                empty.setVisibility(View.VISIBLE);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
            }



    }
